package mest.controller.hws;

import com.alibaba.fastjson.JSON;
import mest.Dto.hws.BomDto;
import mest.entity.bom;
import mest.service.hws.BomDtoService;
import mest.service.hws.BomhwsService;
import mest.service.system.DBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
物料档案
只有增改查
没有修改功能
*/
@RestController
@RequestMapping("/bom")
@CrossOrigin
public class BomDaoController {

    @Resource
    private BomhwsService bomService;
    @Resource
    private BomDtoService bomDtoService;
    @Autowired
    private DBService dbService;

    //新增物料档案
    @RequestMapping("/insertModel")
    public String insertModeh(@RequestBody bom bom) {
        String theirDb = dbService.getCompanyDB();
        System.out.println(bom.toString());
        System.out.println("新增物料档案");
        Map<String, Object> map = new HashMap<String, Object>();
        return bomService.insert(theirDb,bom);
    }

    //删除物料档案
    @RequestMapping("/delModel")
    public String delModelh(Integer id) {
        System.out.println("删除物料档案");
        String theirDb = dbService.getCompanyDB();
        Map<String, Object> map = new HashMap<String, Object>();
        int num = bomService.deleteByPrimaryKey(theirDb,id);
        if (num > 0) {
            map.put("code", 0);
            map.put("msg", "操作成功");
        } else {
            map.put("code", 1);
            map.put("msg", "操作失败");
        }
        return JSON.toJSONString(map);
    }

    //根据id查询物料档案
    @RequestMapping("/selectById")
    public String selectByIdh(Integer id) {
        System.out.println("根据id查询物料档案");
        String theirDb = dbService.getCompanyDB();
        Map<Object, Object> map = new HashMap<Object, Object>();
        try {
            BomDto bomDto = bomDtoService.selectByPrimaryKey(theirDb,id);
            System.out.println(bomDtoService);
            map.put("code", 0);
            map.put("msg", "success");
            map.put("count", "count");
            map.put("data", bomDto);
        } catch (Exception e) {
            map.put("msg", "fales");
            map.put("errmsg", e.toString());
        }
        return JSON.toJSONString(map);
    }


    //查询全部物料档案
    @RequestMapping("/selectAll")
    public String selectAllh(Integer limit, Integer page, String bomname) {
        System.out.println("查询全部物料档案");
        String theirDb = dbService.getCompanyDB();
        System.out.println(bomname);
        Map<Object, Object> map = new HashMap<Object, Object>();
        try {
            List<BomDto> bomDtos = bomDtoService.selectPrimary(theirDb,limit, (page-1)*limit, bomname);
            int count = bomDtoService.selectPrimarycount(theirDb,bomname);
            map.put("code", 0);
            map.put("msg", "success");
            map.put("count",count );
            map.put("data", bomDtos);
        } catch (Exception e) {
            map.put("msg", "fales");
            map.put("errmsg", e.toString());
            System.out.println(e.toString());
        }
        return JSON.toJSONString(map);
    }


}
